<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String id = request.getParameter("id") == null ? "" : request.getParameter("id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建项目甲供出库":"编辑项目甲供出库" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var flag="<%=flag%>";
		var xm_id="";
		$(function(){
			$("#mSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 domSearch();
		       	} 
		   	}); 
			$("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					domSearch();
				}
			});
			$('#mtb').hide();
			if('1' == '<%=flag%>'){
				   jQuery('#ff').form('load','<%=contextPath%>/springMVC/xm/getJGOutMaterial?id=<%=request.getParameter("id")%>');
			}
	       $('#table').datagrid({
	        	url:contextPath+"/springMVC/xm/getXMFlowOutDetail?request_id=<%=id%>",
				rownumbers:true,
				fitColumns:true,
				frozenColumns:[[
				    <%
				    if(!"1".equals(isShow)){
				    %>
					{field:'ck',checkbox:true},
					<%
				    }
					%>
				    {title:'产品id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品编号',field:'code',width:150,align:'center',editor:{
						type:'combogrid',
						options:{
							url: '<%=contextPath %>/springMVC/warehouse/listPageJGMaterial',
						    panelWidth:750,
						    panelHeight:355,
						    rownumbers:true,
						    fitColumns:true,
						    columns: [[  
						       {field:'id',title:'主键',hidden:true},  
						       {field:'material_type_name',title:'产品类别',width:100,align:'center'},
						       {field:'code',title:'产品编号',width:100,align:'center'},
						       {field:'name',title:'产品名称',width:150,align:'center'},
						       {field:'format_type',title:'规格/型号',width:100,align:'center'},
						       {field:'unit_name',title:'单位',width:50,align:'center'},
						       {field:'db_num',title:'库存数量',width:50,align:'center'}
						    ]],
						    toolbar:'#mtb',
						    pageList : [10,50,100],  
							pagination:true,
							onBeforeLoad:function(param){
								var xm_id=jQuery("#xm_id").val();
								param.xm_id=xm_id;
							},
							onClickRow:function(rowIndex,rowData){
								if(checkSameProduct1(rowData,$('#table').datagrid('getData').rows)){
									var pRow = $('#table').datagrid('getRows')[lastIndex];
									pRow.material_id= rowData.id;
									pRow.code = rowData.code;
									pRow.name = rowData.name;
									pRow.unit_name = rowData.unit_name;
									pRow.db_num = rowData.db_num;
									pRow.format_type = rowData.format_type;
									$('#table').datagrid('getData').rows[lastIndex]=pRow;
									var data = $('#table').datagrid('getData');
									$('#table').datagrid('loadData',data);
									
									$('#table').datagrid('endEdit', lastIndex);
									$('#table').datagrid('beginEdit', lastIndex);
									
									//lastIndex =-1;
								}else{
									$.messager.alert("提示","选择的产品已经存在");
								}
							}
						}
					}},
					{title:'产品名称',field:'name',width:300,align:'center'},
					{title:'规格/型号',field:'format_type',width:200,align:'center'},
					{title:'单位',field:'unit_name',width:100,align:'center'},
					{title:'库存数量',field:'db_num',width:100,align:'center'},
					{title:'出库数量',field:'num',width:200,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2,
							myFlag:'1',
							backFunction:'addRow'
						}
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'num'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
						id:'add',
						text:'选择材料',
						disabled:false,
						iconCls:'icon-add',
						handler:function(){
							var xm_id=jQuery("#xm_id").val();
							if(xm_id==''){
								$.messager.alert("提示","请先选择项目！");
								return;
							}
							addRow();
						}
					},'-',{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = $('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = $('#table').datagrid('getRowIndex', row);
								$('#table').datagrid('deleteRow', index);
							}
						}
						var data = $('#table').datagrid('getData');
						$('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
			
		});

		function domSearch()
		{
			var mSearchValue = $('#mSearchValue').val();
			var material_type_id = $('#material_type_id').combotree('getValue');
			$(combogridTarget).combogrid('grid').datagrid('load',{
				searchValue:mSearchValue,
				material_type_id:material_type_id
				});
		}
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
				top:'1000',
				low:'0',
				num:'0'
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		
		function addXMMaterialRequest(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			var xm_id =jQuery("#xm_id").val();	
			var xm_name=jQuery("#xm_name").val();	
			var xm_code=jQuery("#xm_code").val();
			var warehouse_id = $("#warehouse_id").val();
			var warehouse_name = $("#warehouse").val();
			var remark=encodeURIComponent(jQuery('#remark').val());
			if(!checkNullOREmpty(warehouse_id,'仓库为必填项')){return;}
			if(!checkNullOREmpty(xm_id,'项目为必填项')){return;}
			
			var customer_id =jQuery("#customer_id").val();		
	
			var dept =jQuery("#dept_name").val();	
			var dept_id =jQuery("#dept_id").val();	
			var customer =jQuery("#customer").val();	
			var products="";
			var jsondata=$("#table").datagrid("getData");
			for(var i=0;i<jsondata.total;i++){
				if(products!=""){
					products+=";";
				}
				products+=jsonToStr(jsondata.rows[i]);
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				if(jsondata.rows[i].name == null || jsondata.rows[i].name == ""){
					$.messager.alert('提示','第'+index+'行未选择产品！');
					return ;
				}else if(jsondata.rows[i].num == null || jsondata.rows[i].num == "" || jsondata.rows[i].num <= 0){
					$.messager.alert('提示','第'+index+'行数量填写不正确！');
					return ;
				}else if( parseFloat(jsondata.rows[i].num) > parseFloat(jsondata.rows[i].db_num)){
					$.messager.alert('提示','第'+index+'行数量不能大于库存！');
					return ;
				}
				else{
					//判断该产品在该仓库是否存在，如果存在给出提示。避免重复添加
					
				}
			}
			if(!checkNullOREmpty(products,'产品为必填项')){return;}
			var param = "warehouse_id="+warehouse_id+"&warehouse_name="+warehouse_name+"&customer_id="+customer_id+"&customer="+customer+"&remark="+remark
			+"&xm_id="+xm_id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&dept_id="+dept_id+"&dept="+dept+"&products="+products+"&flag=1";
			$(obj).hide();
			$.ajax({
				type:'POST',
				<%=isAdd?"url:'"+contextPath+"/springMVC/xm/addBeforeJGOutMaterialDB',":"url:'"+contextPath+"/springMVC/xm/updateBeforeJGOutMaterialDB?id="+id+"',"%>
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						if(flag!=1){
							eval('parent.window.opener.rtCallBack();');
							parent.window.close();
							}else{
								eval('window.opener.rtCallBack();');
								window.close();
							}
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					$(obj).show();
				}
			});
		}
		function choseXm(){
		    	openDialogResize("<%=contextPath %>/springViews/xm/choseMyXM.jsp?statusType=8",1000, 800);
		    	
		}
		function rtCallBackXmSet(xm_id,xm_code,xm_name,deptId,dept,customer_id,customer){
				jQuery("#xm_id").val(xm_id);
			    jQuery("#xm_name").val(xm_name);
			    jQuery("#xm_code").val(xm_code);
			    jQuery("#dept_name").val(dept);
			    jQuery("#dept_id").val(deptId);
			    jQuery("#customer_id").val(customer_id);
			    jQuery("#customer").val(customer);
			    var url='<%=contextPath %>/springMVC/warehouse/getWareHouseById?type=1&dept_id='+deptId;
				$.ajax({
					type:'GET',
					url:url,
					dataType:'json',
					success:function(rtJson){
						 jQuery("#warehouse_id").val(rtJson.id);
						 jQuery("#warehouse").val(rtJson.name);
					}
				});

		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly  >
						<input type="hidden" id="xm_id" name="xm_id" readOnly  >
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:false" onclick="choseXm();"></a>								</td>
	
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly >
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>项目部门：</td>
					<td class="td2" nowrap>
						<input  id="dept_name" name="dept_name" readOnly >
						<input  type="hidden" id="dept_id" name="dept_id" readOnly  >
					</td>
					<td align="right" class="td1" nowrap>申请人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
				</tr>
				<tr>

					<td align="right" class="td1" nowrap>对应客户：</td>
					<td class="td2" nowrap>
						<input  id="customer" name="customer" readOnly  >
						<input  type="hidden" id="customer_id" name="customer_id" readOnly >
					</td>
				<td align="right" class="td1" nowrap>仓库：</td>
					<td class="td2" nowrap>
						<input  id="warehouse_id" name="warehouse_id" type="hidden">
						<input  id="warehouse" name="warehouse" readonly="readonly" >
					</td>	
				</tr>

				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				<%if(!"1".equals(isShow)){ %>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addXMMaterialRequest(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:parent.window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<%} %>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
			<div id="mtb" style="padding:3px;height:auto">
				<div align="right">
					分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
					查询条件：<input id="mSearchValue" name="mSearchValue" style="width:150px" title="材料编码或名称"/>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="domSearch();"></a>
				</div>
			</div>
		
	</body>
</html>